package com.ysd.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Userchecks;
import com.ysd.service.UserchecksService;
import com.ysd.util.Result;

@RestController
public class UserchecksController {
	@Autowired
	UserchecksService uS;
	@Resource
	private SimpleDateFormat dateFormat;
	
	@RequestMapping("getAskers")
	public Object getAskers(Userchecks u) {
		u.setPage((u.getPage()-1)*u.getPage());
		List<Userchecks> list=uS.findUserChecks(u);
		if (list!=null) {
			int n=uS.findCount(u);
			return Result.toClient("0", "查询成功", n, list);
		}
		return Result.toClient("1", "查询失败");
	}
	@RequestMapping("xgCheckState")
	public Object xgCheckState(Userchecks u) throws ParseException  {
		Userchecks ask=uS.findUCk(u);
		Date  currdate = dateFormat.parse(u.getCheckInTime());
		 System.out.println(currdate);
		 long t=1000*60*60*24*7;
		 Date afterDate = new Date(currdate .getTime() + t);
		 Date nowss=new Date();
//		 String qiTian=dateFormat.format(afterDate);
//		 String nows=dateFormat.format(nowss);  
		if (ask==null) {
			if (nowss.after(afterDate)) {
				return Result.toClient("1", "距签到时间已超过七天,签退失败");
			}
				int n=uS.xgCheckState(u);
				if (n>0) {
					return Result.toClient("0", "签退成功");
				}
				return Result.toClient("1", "签退成功");	
		}
		return Result.toClient("1", "已经签退不能再次签退");
	}
	@RequestMapping("qianDao")
	public Object qianDao(Userchecks u) {
		Userchecks ask=uS.findUseState(u);
		if (ask==null) {
			int n=uS.tjCheckState(u);
			if (n>0) {
				return Result.toClient("0", "签到成功");
			}
			return Result.toClient("1", "签到失败");
		}
		return Result.toClient("1", "今日已经签过到了");
	}
	@RequestMapping("geRenQianTui")
	public Object geRenQianTui(Userchecks u) {
		Userchecks useC=uS.findOne(u);
		if (useC==null) {
			int n=uS.gRQianTui(u);
			if (n>0) {
				return Result.toClient("0", "签退成功");
			}
			return Result.toClient("1", "签退失败");
		}
		return Result.toClient("1", "今日已经签退过");
	}
	
	@RequestMapping("isQianDao")
	public Object isQianDao(Userchecks u) {
		Userchecks ask=uS.findUseState(u);
		if (ask==null) {
			return Result.toClient("0", "没签到");
		}
		return Result.toClient("1", "今日已经签过到了");
	}
}
